package com.farm.controller;



import com.farm.aop.Operation;
import com.farm.aop.Type;
import com.farm.aop.aopUpdate;
import com.farm.common.R;
import com.farm.mapper.AnswerMapper;
import com.farm.mapper.CommitMapper;
import com.farm.mapper.LoveMapper;
import com.farm.pojo.Answer;
import com.farm.pojo.Commited;
import com.farm.pojo.DTO.InquiryDto;
import com.farm.pojo.Logs;
import com.farm.pojo.Vo.QuestionVo;
import com.farm.pojo.Vo.ReplyVo;
import com.farm.pojo.Vo.CollectedVo;
import com.farm.pojo.love;
import com.farm.service.CommitService;
import com.farm.service.LogsService;
import jdk.nashorn.internal.ir.ReturnNode;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.PublicKey;
import java.util.List;

/**
 * <p>
 * 发布文章 前端控制器
 * </p>
 *
 * @author farm
 * @since 2023-10-30
 */
@RestController
@RequestMapping("/commit")
@Slf4j
public class CommitController {

    @Autowired
    private CommitService commitService;
    @Autowired
    private CommitMapper commitMapper;

    @Autowired
    private AnswerMapper answerMapper;

    @Autowired
    private LogsService logsService;

    public void logsKeep(){
        Logs logs = new Logs();
        logs.setOperate("对问答进行操作");
        logsService.save(logs);
    }
    /**
     * 发布问答
     * @param questionVo
     * @return
     */
    @PostMapping("/postQuestion")
    public R<String> PostQuestion(@RequestBody QuestionVo questionVo){
        log.info("开始接收发布问题数据：");
        log.info(questionVo.toString());
        if (questionVo.getContent().equals("")){
            return R.success("请输入问答内容");
        }
        commitService.postQuestion(questionVo);
        logsKeep();
        return R.success("发布成功");
    }


    /**
     * 查看问答详情
     * @return
     */
    @GetMapping("/showInquiry")
    public R<List<InquiryDto>> showInquiry(String partitions){
        log.info("开始查询问答详情");
        List<InquiryDto> inquiryDtoList = commitService.showInquiry(partitions);
        return R.success(inquiryDtoList);
    }

    /**
     * 添加评论
     * @param replyVo
     * @return
     */
    @PostMapping("/addReply")
    public R<String> addReply(@RequestBody ReplyVo replyVo) {
        log.info("开始添加评论：" + replyVo.toString());
        if(replyVo.getRespond().equals("")){
            return R.success("请填写评论内容");
        }
        commitService.addReply(replyVo);
        return R.success("评论成功，等待审核");
    }

    /**
     * 添加和修改点赞
     * @param commitId
     * @param liking
     */
    @PostMapping("/addLove")
    public void addLove(@RequestParam Long commitId, @RequestParam Integer liking){
        if (commitId.equals("")){
            return;
        }
        log.info("点赞了：" + commitId);
        commitService.addLove(commitId, liking);
    }

    /**
     * 收藏功能
     * @param collectedVo
     */
    @PostMapping("/collected")
    public void collected(@RequestBody CollectedVo collectedVo){
        if (collectedVo.getCommitId().equals("")){
            return;
        }
        log.info("收藏了：" + collectedVo.getCommitId());
        commitService.collected(collectedVo);
    }

    /**
     * 我的发布
     * @return
     */
    @GetMapping("/MyInquiry")
    public R<List<InquiryDto>> MyInquiry(){
        log.info("开始查询我的问答");
        List<InquiryDto> inquiryDtoList = commitService.MyInquiry();
        log.info("我的问答：" + inquiryDtoList);
        System.out.println("我的发布：" + inquiryDtoList);
        return R.success(inquiryDtoList);
    }

    /**
     * 根据问答id删除问答
     * @param commitId
     * @return
     */
    @DeleteMapping("/deleteByCommitId/{commitId}")
    @aopUpdate(TypeValue = Type.COMMITED, OperationValue = Operation.DELETE)
    public R<String> deleteByCommitId(@PathVariable Long commitId){
        log.info("开始删除操作：" + commitId);
        commitService.deleteByCommitId(commitId);
        logsKeep();
        return R.success("删除成功");
    }


    /**
     * 我的收藏
     * @return
     */
    @GetMapping("/MyCollection")
    public R<List<InquiryDto>> MyCollection(){
        log.info("开始查询我的问答");
        List<InquiryDto> inquiryDtoList = commitService.MyCollection();
        return R.success(inquiryDtoList);
    }

    /**
     * 不喜欢
     * @param commitId
     * @param dislike
     */
    @PostMapping("/dislike")
    public void disliked(@RequestParam Long commitId, @RequestParam Integer dislike){
        if (commitId.equals("")){
            return;
        }
        System.out.println("不喜欢：" + commitId + " 状态：" + dislike);
        commitService.disliked(commitId, dislike);
    }

    /**
     * 管理员获取全部数据
     * @return
     */
    @GetMapping("/admin/getlist")
    public R<List<Commited>> selectAll(){
        log.info("我开始全部查询管理员的数据啦：");
        List<Commited> commiteds = commitMapper.selectList(null);
        if(commiteds != null && commiteds.size() > 0){
            return R.success(commiteds);
        }
        return R.success(null);
    }

    /**
     * 更新用户状态
     * @param commited
     * @return
     */
    @PostMapping("/admin/update")
    @aopUpdate(TypeValue = Type.COMMITED, OperationValue = Operation.UPDATE)
    public R<String> updateState(@RequestBody Commited commited){
        log.info("我来更新审核状态了：" + commited);
        commitMapper.updateById(commited);
        logsKeep();
        return R.success("更新状态成功");
    }


    /**
     * 管理员获取评论数据
     * @return
     */
    @GetMapping("/admin/getAnswerAll")
    public R<List<Answer>> selectLoveAll(){
        log.info("我开始全部查询管理员的数据啦：");
        List<Answer> answerList = answerMapper.selectList(null);
        if(answerList != null && answerList.size() > 0){
            return R.success(answerList);
        }
        return R.success(null);
    }


    /**
     * 更新评论
     * @param answer
     * @return
     */
    @PutMapping("/admin/updateAnswer")
    public R<String> updateAnswerState(@RequestBody Answer answer){
        log.info("我来更新审核状态了：" + answer);
        logsKeep();
        answerMapper.updateById(answer);
        return R.success("更新状态成功");
    }

    /**
     * 删除评论
     * @param id
     * @return
     */
    @DeleteMapping("/deleteAnswer/{id}")
    public R<String> deleteAnswer(@PathVariable Long id){
        log.info("我是删除的评论：" + id);
        answerMapper.deleteById(id);
        return R.success("删除成功");
    }



}

